/**
 * Created by Vivienfanghua on 2016/2/16.
 */
function drawGraph(arguments) {
    var svg_root_name = arguments["svg_root_name"];
    var data = arguments["data"];
    var graph_type = arguments["graph_type"];
    var graph_color_band = arguments["graph_color_band"];
    var graph_attr = arguments["graph_attr"];
    var is_prop = arguments["is_prop"];
    var graph_prop_num = arguments["graph_prop_num"];
    var graph_size_range = arguments["graph_size_range"];
    var graph_prop_attr = arguments["graph_prop_attr"];
    var graph_size_one = arguments["graph_size_one"];
    var bar_color_band = arguments["bar_color_band"];
    var bar_per_width = arguments["bar_per_width"];
    var bar_per_height = arguments["bar_per_height"];
    var bar_height_map = arguments["bar_height_map"];
    var bar_type = arguments["bar_type"];
    var bar_attr = arguments["bar_attr"];
    switch (graph_type) {
        case "pieChart":
            drawPie(svg_root_name, data, graph_color_band, graph_attr, is_prop, graph_prop_num, graph_size_range, graph_prop_attr, graph_size_one);
            break;
        case "barChart":
            drawBar(svg_root_name, bar_type, data, bar_color_band, bar_attr, bar_per_width, bar_per_height, bar_height_map);
            break;
    }
}

function drawPie(svg_root_name, data, graph_color_band, graph_attr, is_prop, graph_prop_num, graph_size_range, graph_prop_attr, graph_size_one) {
    $("#分区统计图").html("");
    var group = Snap("#地图").g();
    group.attr({id: "分区统计图"});
    var color = getColor(colormap[graph_color_band]);
    var graph_data = [];
    graph_attr.forEach(function (e) {
        graph_data.push(data.get_attr_name(e.trim()))
    });
    var code = data.get_attr_name("编码");
    if (is_prop) {
        var data_prop = data.get_attr_name(graph_prop_attr.trim());
        var sizearray = getCData(graph_size_range[0], graph_size_range[1], data_prop);
        for (var i = 0; i < data.row_num; i++) {
            var graph_data_i = [];
            graph_data.forEach(function (e) {
                graph_data_i.push(e[i])
            });
            var code_i = "Prov_P_" + code[i];
            var x = parseInt($("#" + code_i).attr("cx"));
            var y = parseInt($("#" + code_i).attr("cy"));
            var piegraph = pieChart("#" + svg_root_name, "Pie" + i, graph_data_i, x, y, sizearray[i], color);
            group.add(piegraph);
        }
    }
    else {
        for (var i = 0; i < data.row_num; i++) {
            var graph_data_i = [];
            graph_data.forEach(function (e) {
                graph_data_i.push(e[i])
            });
            var code_i = "Prov_P_" + code[i];
            var x = parseInt($("#" + code_i).attr("cx"));
            var y = parseInt($("#" + code_i).attr("cy"));
            var piegraph = pieChart("#" + svg_root_name, "Pie" + i, graph_data_i, x, y, graph_size_one, color);
            group.add(piegraph);
        }
    }
}

function drawBar(svg_root_name, bar_type, data, graph_color_band, graph_attr, per_width, per_height, height_map) {
    $("#分区统计图").html("");
    var group = Snap("#地图").g();
    group.attr({id: "分区统计图"});
    var color = getColor(colormap[graph_color_band]);
    var graph_data = [];
    graph_attr.forEach(function (e) {
        graph_data.push(data.get_attr_name(e.trim()))
    });
    var code = data.get_attr_name("编码");
    for (var i = 0; i < data.row_num; i++) {
        var graph_data_i = [];
        graph_data.forEach(function (e) {
            graph_data_i.push(e[i])
        });
        var code_i = "Prov_P_" + code[i];
        var x = parseInt($("#" + code_i).attr("cx"));
        var y = parseInt($("#" + code_i).attr("cy"));
        var bargraph = barChart("#" + svg_root_name, "Bar" + i, graph_data_i, x, y, per_height, per_width, color);
        group.add(bargraph);
    }
}

function drawLevel(level_color_band, level_num, level_method, data, code) {
    var rate = Rating(data, level_num, level_method);
    for (var i = 0; i < data.length; i++) {
        var code_i = "Prov_R_" + code[i];
        var area_one = document.getElementById(code_i);
        if (area_one.children.length == 0)
            setcolor(code_i, colormap[level_color_band][rate["type"][i]]);
        else
            setcolor2(code_i, colormap[level_color_band][rate["type"][i]]);
    }
}

function drawSymbol(arguments){
    var svg_root_name=arguments["svg_root_name"];
    var data=arguments["data"];
    var symbol_attr=arguments["symbol_attr"];
    var symbol_size_range=arguments["symbol_size_range"];
    var symbol_svg=arguments["symbol_svg"];
    var data_prop = data.get_attr_name(symbol_attr.trim());
    var sizearray = getCData(symbol_size_range[0], symbol_size_range[1], data_prop);
    var code = data.get_attr_name("编码");
    var svg = Snap("#"+svg_root_name);
    $("#分区统计图")&&$("#分区统计图").remove();
    var g = Snap("#地图").g();
    g.attr({id:"分区统计图"});
    Snap.ajax("/media/symbolsvg/"+symbol_svg+".svg", function (req) {
        Snap("defs").append(Snap.parse(req.responseText));
        for (var i = 0; i < data.row_num; i++) {
            var code_i = "Prov_P_" + code[i];
            var x = parseInt($("#" + code_i).attr("cx"));
            var y = parseInt($("#" + code_i).attr("cy"));
            var ele_string=Snap.format('<use xlink:href="#{symbol_svg}" x="{x}" y="{y}" width="{width}" height="{height}"></use>',{symbol_svg:symbol_svg,x:x-sizearray[i]/2,y:y-sizearray[i]/2,width:sizearray[i],height:sizearray[i]});
            //var item='<use xlink:href="#'+symbol_svg+'" x="'+x+'" y="'+y+'" width="'+sizearray[i]+'" height="'+sizearray[i]+'"></use>';
            var use=Snap.parse(ele_string);
            g.add(use);
        }
    });
}
function onloadsvg(filename, id) {
    Snap.load(filename, function (svg) {
        $("#" + id).empty();
        this.appendChild(svg.node);
        var svg_id=$("svg").attr("id");
        var svg = Snap("#" + svg_id);
        var widthstring = svg.attr("width");
        var heightstring = svg.attr("height");
        var iniWidth = parseFloat(widthstring.substr(0, widthstring.length - 2));
        var iniHeight = parseFloat(heightstring.substr(0, heightstring.length - 2));
        var scalewidth = 800 / iniWidth;
        var scaleheight = 600 / iniHeight;
        var scale = scaleheight < scalewidth ? scaleheight : scalewidth;
        //svg.attr({width:iniWidth*scale+"px",height:iniHeight*scale+"px"});
        svg.attr({width: "100%", height: "100%"});
    }, document.getElementById(id));
}